
//--------------------------------------------------------------------------------------------------------------------------
// Attributes
//--------------------------------------------------------------------------------------------------------------------------

function GUIAttribute() {}


function GUIAttribute(name, dataValue, dataType) {
  this.name = name;
  this.dataType = dataType;
  this.dataValue = dataValue;
  
  this.Validate = GUIAttribute_Validate;
  this.toJSON = GUIAttribute_toJSON;
  this.serialize = GUIAttribute_serialize;
  this.addToForm = GUIAttribute_addToForm;
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttribute_toJSON() {
  return "{\"name\": \"" + serializeValue(this.name) + "\", \"data_type\": \"" + serializeValue(this.dataType) + "\", \"data_value\": \"" + serializeValue(this.dataValue) + "\"}";
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttribute_serialize(index)
{
	return "&attribute" + index + "_name=" + serializeValue(this.name) + "&attribute" + index + "_data_value=" + serializeValue(this.dataValue) + "&attribute" + index + "_data_type=" + serializeValue(this.dataType);
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttribute_addToForm(form,dataObj,index) {
    dataObj.serverInterface.addHiddenInput(form,"attribute" + index + "_name",this.name);
		dataObj.serverInterface.addHiddenInput(form,"attribute" + index + "_data_value",this.dataValue);
		dataObj.serverInterface.addHiddenInput(form,"attribute" + index + "_data_type",this.dataType);
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttribute_Validate(pattern) {
  switch (this.dataType) {
      case "DateTime":
          break;
    
      case "Boolean":
          break;
    
      case "Text":
          break;
    
      case "LongText":
          break;
    
      case "Int":
          break;
    
      case "Double":
          break;
    
      case "File":
          break;
  }
}

//--------------------------------------------------------------------------------------------------------------------------

// Attributes list

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes() {
  this.attributes = [];
  this.setAttribute = GUIAttributes_setAttribute;
  this.setAttributes = GUIAttributes_setAttributes;
  this.getAttribute = GUIAttributes_getAttribute;
  this.toJSON = GUIAttributes_toJSON;
  this.serialize = GUIAttributes_serialize;
  this.applyOnAttributes = GUIAttributes_applyOnAttributes;
  this.addToForm = GUIAttributes_addToForm;
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_setAttribute(name, value, dataType) {
  var value2 = "";
  if (value && value != "undefined") 
    {value2 = value;}
  this.attributes[name] = new GUIAttribute(name, value2, dataType);
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_setAttributes(attributesIn)
{
  applyOnArray(this,attributesIn,function(_this,attribute) {
                                    _this.attributes[attribute.name] = new GUIAttribute(attribute.name, attribute.data_value, attribute.data_type);
                                });
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_getAttribute(name) {
  return this.attributes[name];
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_toJSON() {
  var tmpstr = "";
  var separ = "[";
  this.applyOnAttributes(function(_this,attribute){
                            tmpstr += separ + attribute.toJSON();
                            separ = ",";
                          });
  return tmpstr + "]";
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_serialize() {
  var tmpstr = "";
  var cpt = 1;
  this.applyOnAttributes(function(_this,attribute){
                            tmpstr += attribute.serialize(cpt);
                            cpt++;
                          });  
  return tmpstr;
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_addToForm(form,dataObj) {
  var tmpstr = "";
  var cpt = 1;
  this.applyOnAttributes(function(_this,attribute){
                            attribute.addToForm(form,dataObj,cpt);
                            cpt++;
                          });  
  return tmpstr;
}

//--------------------------------------------------------------------------------------------------------------------------

function GUIAttributes_applyOnAttributes(functionToApply) {
    applyOnArray(this,this.attributes,functionToApply);
}

